package studiodiva.portfolio.client.view.particleengine;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.dom.client.ImageElement;

class Particle {

  static final int FOV = 60;
  private static final int SCREEN_HALF_HEIGHT = MouseParticles.SCREEN_HEIGHT / 2;
  private static final int SCREEN_HALF_WIDTH = MouseParticles.SCREEN_WIDTH / 2;
  private final Vector position = new Vector(0, 0, 0);
  private final Vector vel = new Vector(0, 0, 0);
  private boolean enabled = true;

  Particle() {
    reset();
  }

  public void disable() {
    enabled = false;
  }

  public Vector getPos() {
    return position;
  }

  public boolean isEnabled() {
    return enabled;
  }

  void draw(Context2d canvas2d, ImageElement image) {
    double x3d = position.getX();
    double y3d = position.getY();
    double z3d = position.getZ();
    double scale = FOV / (FOV + z3d);
    double x2d = x3d * scale + SCREEN_HALF_WIDTH;
    double y2d = y3d * scale + SCREEN_HALF_HEIGHT;
    scale *= 6;
    if (scale > 0) canvas2d.drawImage(image, x2d - scale, y2d - scale, scale * 2, scale * 2);
  }

  void reset() {
    position.reset(0, 50, 0);
    vel.reset(Math.random() * 20 - 10, Math.random() * -5, Math.random() * 20 - 10);
    enabled = true;
  }

  void update(double drag, int gravity) {
    if (enabled) {
      position.plusEq(vel);
      vel.multiplyEq(drag);
      vel.addY(gravity);
    }
  }
}